package com.tanhua.manage.listener;

import com.tanhua.commons.templates.HuaWeiUGCTemplate;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.dubbo.api.mongo.PublishApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RocketMQMessageListener(topic = "tanhuan-publish-151",consumerGroup = "tanhuan-publish-151-group")
@Slf4j
public class PublishMessageListener implements RocketMQListener<String> {

    @Reference
    private PublishApi publishApi;
    @Autowired
    private HuaWeiUGCTemplate huaWeiUGCTemplate;

    @Override
    public void onMessage(String publishId) {
        log.info("动态审核，动态id是{}",publishId);
        // 根据动态id查询
        Publish publish = publishApi.findById(publishId);
        // 获取动态中文本内容
        String textContent = publish.getTextContent();
        // 动态审核：文本内容审核
        boolean contentCheck = huaWeiUGCTemplate.textContentCheck(textContent);
        Integer state = 2;
        // 判断
        if (contentCheck) {
            // 内容审核通过，接下来需要图片审核
            List<String> medias = publish.getMedias();
            // 动态审核：图片审核
            boolean imgCheck = huaWeiUGCTemplate.imageContentCheck(medias.toArray(new String[]{}));
            // 判断
            if (imgCheck) {
                // 动态审核，审核通过
                state = 1;
            }
        }
        // 更新动态表
        publishApi.updateState(publish.getId().toString(),state);

        log.info("动态审核，动态id是{},审核成功",publishId);
    }
}
